﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using LiveWriterMp3Player.Properties;

namespace LiveWriterMp3Player
{
    public partial class InsertMp3Dialog : Form 
    {
        public InsertMp3Dialog()
        {
            InitializeComponent();

            this.mp3UrlTextbox.Text = Settings.Default.LastInsertedMp3;
            this.playerUrlTextbox.Text = Settings.Default.PlayerUrl;
            this.playerWidthTextbox.Text = Settings.Default.PlayerWidth.ToString();
            this.playerHeightTextBox.Text = Settings.Default.PlayerHeight.ToString();
        }

        public string Url
        {
            get { return this.mp3UrlTextbox.Text; }
        }

        public string PlayerUrl
        {
            get { return this.playerUrlTextbox.Text; }
        }

        public int PlayerWidth
        {
            get { return GetPlayerWidthOrDefault(); }
        }

        public int PlayerHeight
        {
            get { return GetPlayerHeightOrDefault(); }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);

            if (this.DialogResult == DialogResult.OK)
            {
                Settings.Default.LastInsertedMp3 = this.mp3UrlTextbox.Text;
                Settings.Default.PlayerUrl = this.playerUrlTextbox.Text;
                Settings.Default.PlayerWidth = GetPlayerWidthOrDefault();
                Settings.Default.PlayerHeight = GetPlayerHeightOrDefault();
                Settings.Default.Save();
            }
        }

        private int GetPlayerHeightOrDefault()
        {
            const int defaultHeight = 24;
            int height;
            return int.TryParse(this.playerHeightTextBox.Text, out height) ? height : defaultHeight;
        }

        private int GetPlayerWidthOrDefault()
        {
            const int defaultWidth = 290;
            int width;
            return int.TryParse(this.playerWidthTextbox.Text, out width) ? width : defaultWidth;
        }

        private void insertButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void advancedButton_Click(object sender, EventArgs e)
        {
            bool shouldBeExpanded = !this.panel1.Visible;
            this.panel1.Visible = shouldBeExpanded;
            this.advancedButton.Text = shouldBeExpanded ? "Advanced <<" : "Advanced >>";
        }
    }
}
